
from datetime import timedelta

def query_page_list(model_class, page_size: int = 10, page_no: int = 1, params: dict = None, order_by = None, filters: list = None, max_page_size: int = 9999):
    """查询运动记录, 分页查询"""

    params = params or {}
    page_no = max(page_no, 1)
    page_size = min(max(page_size, 1), max_page_size)

    query = model_class.query

    # 查询前的筛选
    if filters is not None:
        for filterObj in filters:
            query = query.filter(filterObj)

    # 拼接查询条件
    for key, value in params.items():
        if value is not None and hasattr(model_class, key):
            column = getattr(model_class, key)  # 明确指定当前模型的字段
            if isinstance(value, dict) and '$like' in value:
                query = query.filter(column.like(value['$like']))
            elif isinstance(value, dict) and '$in' in value:
                query = query.filter(column.in_(value['$in']))
            else:
                query = query.filter(column == value)  # 确保字段来自主模型

    # 排序
    if order_by is not None:
        query = query.order_by(order_by)

    total = query.count()

    offset = (page_no - 1) * page_size
    records = query.offset(offset).limit(page_size).all()

    return {
        'total': total,
        'records': [record.to_dict() for record in records],
        'pageNo': page_no,
        'pageSize': page_size,
        'total_pages': (total + page_size - 1) // page_size
    }

def to_beijing_time(utc_time):
    """
    将 UTC 时间转换为北京时间（UTC+8）
    :param utc_time: datetime 对象，表示 UTC 时间
    :return: datetime 对象，表示北京时间
    """
    return utc_time + timedelta(hours=8)